<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper" id="queryForm">
      <a-form layout="inline">
        <a-row :gutter="24">
          <a-col :md="6" :sm="24">
            <a-form-item label="工装编号">
              <a-input placeholder="输入工装编号" v-model="queryParam.fixtureCode"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="物料编码">
              <a-input placeholder="输入物料编码" v-model="queryParam.materialCode"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="状态">
              <a-select placeholder="设备状态" v-model="queryParam.fixtureStatus" default-value="-1">
                <a-select-option value="2">制作中</a-select-option>
                <a-select-option value="3">调试中</a-select-option>
                <a-select-option value="4">等待验收</a-select-option>
                <a-select-option value="5">已入库</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <!--  <a-dropdown>
          <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down"/></a-button>
          <a-menu slot="overlay">
            <a-menu-item key="1" @click="batchPrint"><a-icon type="delete" />批量打印标签</a-menu-item>
          </a-menu>
        </a-dropdown>
        <a-button v-show="false" v-print="'#table'" ref="printBtn">打印</a-button>
        <a-button @click="goPrint">打印</a-button>
        -->
      <a-upload
        name="file"
        :showUploadList="false"
        :multiple="false"
        :headers="tokenHeader"
        :action="importExcelUrl"
        @change="handleImportExcel"
      >
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <a-button type="primary" icon="download" @click="handleExportXls('工装列表')">导出</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
        <span style="float:right;">
          <a @click="loadData()"><a-icon type="sync" />刷新</a>
        </span>
      </div>

      <a-table
        id="table"
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, onSelect: onSelectRow }"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record">
          <a @click="handleOpenFixtureCard(record)">详情</a>
          <!--          <a-divider type="vertical" />-->
          <!--          <a @click="handlePrint(record)">打印标签</a>-->
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down"/></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a-popconfirm title="确认工装制作完成了吗?" @confirm="() => handleMakeDone(record.id)">
                  <a-icon type="tool" /><a>制作</a>
                </a-popconfirm>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确认调试完成了吗?" @confirm="() => handleDebugDone(record.id)">
                  <a-icon type="bug" /><a>调试</a>
                </a-popconfirm>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确认验收完成了吗?" @confirm="() => handleQualityDone(record.id)">
                  <a-icon type="check-circle" /><a>验收</a>
                </a-popconfirm>
              </a-menu-item>
              <a-menu-item style="display: flex; align-items: center;" @click="handleInWarehouseDone(record)">
                <a-icon type="safety" /><a style="color: #1890FF;">入库</a>
                <!-- <a-popconfirm title="确认入库吗?" @confirm="() => handleDelete(record.id)">
                  </a-popconfirm> -->
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->
    <FixtureToolModal ref="fixtureDetailModal"></FixtureToolModal>
    <!-- 入库表单-start -->
    <InWareHouseModal ref="inWareHouseModal" :params="inWraeHouseObj" @ok="updateList"></InWareHouseModal>
    <!-- 入库表单-end -->
    <!-- 打印表格-start -->

    <!-- 打印表格-end -->
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import FixtureToolModal from '@views/genbyte/modal/FixtureToolModal'
import InWareHouseModal from '@views/genbyte/modal/InWareHouseModal'
import { updateFixStatusMakeDone, updateFixStatusDebugDone, updateFixStatusQualityDone } from '@api/api'
import { getAction } from '@/api/manage'
import { getMCInfoByIdOrCode } from '@api/api'

export default {
  name: 'FixtrueToolList',
  mixins: [JeecgListMixin],
  components: {
    FixtureToolModal,
    InWareHouseModal
  },
  data() {
    return {
      description: '工装治具列表界面',
      importExcelUrl: `${window._CONFIG['domianURL']}/fixtureToolCtrl/importExcel`,
      // 表头
      columns: [
        {
          title: '序号',
          key: 'rowIndex',
          width: 50,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '名称',
          align: 'left',
          dataIndex: 'name'
        },
        {
          title: '工装编号',
          align: 'left',
          dataIndex: 'fixtureCode'
        },
        {
          title: '物料编码',
          align: 'center',
          dataIndex: 'materialCode'
        },
        {
          title: '制作人',
          align: 'center',
          dataIndex: 'manufactureUser',
          customRender: text => {
            let re = ''
            if (text === null) {
              re = '/'
            } else {
              re = text
            }
            return re
          }
        },
        {
          title: '调试人',
          align: 'center',
          dataIndex: 'debugUser',
          customRender: text => {
            let re = ''
            if (text === null) {
              re = '/'
            } else {
              re = text
            }
            return re
          }
        },
        {
          title: '验收人',
          align: 'center',
          dataIndex: 'qualityUser',
          customRender: text => {
            let re = ''
            if (text === null) {
              re = '/'
            } else {
              re = text
            }
            return re
          }
        },
        {
          title: '制作状态',
          align: 'center',
          dataIndex: 'fixtureStatus',
          customRender: text => {
            let re = ''
            if (text === 1) {
              re = '生成标签'
            } else if (text === 2) {
              re = '制作'
            } else if (text === 3) {
              re = '调试'
            } else if (text === 4) {
              re = '验收'
            } else if (text === 5) {
              re = '入库'
            }
            return re
          }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      //选中的列表行数组
      selectedArr: [],
      url: {
        list: '/fixtureToolCtrl/getFixToolList',
        // put: '/fixtureToolCtrl/updateFixStatusQualityDone'
        // deleteBatch: "/test/jeecgOrderMain/deleteBatch",
        exportXlsUrl: '/fixtureToolCtrl/exportXls'
      },
      //入库的工装信息
      inWraeHouseObj: {}
    }
  },
  created() {
    // console.log(this.selectionRows)
    // getMCInfoByIdOrCode({ mcInfoId: this.fixtureToolModel.materialInfoId })
  },
  methods: {
    goPrint() {
      for (var i = 0; i < 3; i++) {
        this.$refs.printBtn.$el.click()
      }
    },
    //查看详情
    handleOpenFixtureCard(record) {
      this.$refs.fixtureDetailModal.open(record)
      //传递参数到子组件
      //this.$refs.fixtureDetailModal.title = "查看工装治具详情";
    },
    handleMakeDone(fixId) {
      updateFixStatusMakeDone({ fixtureId: fixId }).then(res => {
        if (res.success) {
          this.$notification['success']({
            message: '状态更新完成!',
            description: ''
          })
          this.loadData()
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    handleDebugDone(fixId) {
      updateFixStatusDebugDone({ fixtureId: fixId }).then(res => {
        if (res.success) {
          this.$notification['success']({
            message: '状态更新完成!',
            description: ''
          })
          this.loadData()
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    //验收
    handleQualityDone(fixId) {
      updateFixStatusQualityDone({ fixtureId: fixId }).then(res => {
        if (res.success) {
          this.$notification['success']({
            message: '状态更新完成!',
            description: ''
          })
          this.loadData()
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    //入库
    handleInWarehouseDone(record) {
      // console.log('入库', record)
      this.inWraeHouseObj = record
      this.$refs.inWareHouseModal.add()
      this.$refs.inWareHouseModal.title = '入库'
      this.$refs.inWareHouseModal.disableSubmit = false
    },
    onSelectRow(record, selected) {
      if (selected) {
        this.selectedArr.push(record)
        //选中的列表行数
        let length = this.selectedArr.length
        getMCInfoByIdOrCode({ mcInfoId: record.materialInfoId }).then(res => {
          let obj = {}
          obj.voltage = res.result.voltage
          obj.fixtureVer = res.result.fixtureVer
          obj.applicableModel = res.result.applicableModel
          obj.prodVer = res.result.prodVer
          obj.power = res.result.power
          //不仅需要选中的行数据，还需要工装数据，两种数据合并，便于后期循环渲染
          Object.assign(this.selectedArr[length - 1], obj)
        })
      } else {
        this.selectedArr.map((item, index) => {
          record.id === item.id ? this.selectedArr.splice(index, 1) : ''
        })
      }
      console.log(this.selectedArr)
    },
    //批量打印
    batchPrint() {
      console.log('批量打印')
    }
  }
}
</script>

<style scoped></style>
